/*
 * Copyright (c) 2020.
 *  This file is part of Chooser.
 *
 * Chooser is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * Chooser is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with Chooser.  If not, see <http://www.gnu.org/licenses/>.
 *
 */

package tech.waelk.radioactive.choices;

import android.app.Activity;
import android.app.AlertDialog;
import android.content.Context;
import android.content.pm.PackageInfo;
import android.content.pm.PackageManager;
import android.os.Bundle;
import android.text.style.UnderlineSpan;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.MenuItem;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;

import com.binaryfork.spanny.Spanny;

import java.util.Random;

public class MainActivity extends Activity {
    //button and textview
    Button runButton;
    TextView output;
    //rng
    Random random;
    //context
    Context contextActivity;


    //functions
    //set string and color to view
    void setTextViewOutput(String string, int color, TextView textView){
        //this function just sets the text and color of the given textview.
        textView.setTextColor(color);
        textView.setText(string);
    }
    //set the listener
    View.OnClickListener buttonListener = new View.OnClickListener() {
        @Override
        public void onClick(View view) {
            //get the next random value and set the view accordingly.
            if (random.nextBoolean())
                //set to true
                setTextViewOutput(getString(R.string.yes), getColor(R.color.colorYes), output);
            else
                //set to false
                setTextViewOutput(getString(R.string.no), getColor(R.color.colorNo), output);
        }
    };

    //about dialog
    private void aboutDialog() {
        //for version info extraction
        String version = "\n\nVersion Name: ";
        int versionCode = 0;
        try {
            PackageInfo packageInfo = getPackageManager().getPackageInfo(getPackageName(), 0);
            version += packageInfo.versionName;
            version += "\nVersion Code: " + packageInfo.versionCode;
        } catch (PackageManager.NameNotFoundException e) {
            versionCode = -1;
        }
        if (versionCode == -1) {
            version = "";
        }
        //build the dialog
        AlertDialog.Builder builder = new AlertDialog.Builder(contextActivity, R.style.DialogTheme);
        LayoutInflater layoutInflater = this.getLayoutInflater();
        View dialogView = layoutInflater.inflate(R.layout.linear_layout_about_container,null);
        builder.setView(dialogView);
        TextView textView = dialogView.findViewById(R.id.textViewAbout);
        //build the text
        Spanny message = new Spanny(getString(R.string.app_name) + '\n', new UnderlineSpan()).append('\n' + getString(R.string.email)).append('\n' + getString(R.string.copyright)).append('\n' + version + '\n').append("\n\n" + getString(R.string.license)).append("\n\n" + this.getResources().getText(R.string.radioactive));
        builder.setTitle(getString(R.string.about));
        //add the icon
        builder.setIcon(getResources().getDrawable(R.mipmap.ic_launcher, getTheme()));
        //set text
        textView.setText(message.toString());
        //create the object
        AlertDialog dialog = builder.create();
        //display the dialog to the user
        dialog.show();

        //change size
        int width = (int)(contextActivity.getResources().getDisplayMetrics().widthPixels*0.9);
        int height = (int)(contextActivity.getResources().getDisplayMetrics().heightPixels*0.9);
        dialog.getWindow().setLayout(width, height);
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        //set context
        contextActivity = this;
        //get the views
        runButton = findViewById(R.id.button);
        output = findViewById(R.id.textView);

        //rng init
        random = new Random();

        //set the button logic
        runButton.setOnClickListener(buttonListener);
    }

    //overflow menu setup
    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        MenuInflater menuInflater = getMenuInflater();
        menuInflater.inflate(R.menu.menu_main, menu);
        return true;
    }

    //overflow menu logic
    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        switch (item.getItemId()) {
            case R.id.action_about:
                aboutDialog();
                return true;
        }
        return super.onOptionsItemSelected(item);
    }
}
